package org.ns.longhttp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.cookie.*;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import io.netty.handler.codec.http.cookie.ServerCookieEncoder;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.List;

public class ResponseUtil {

    public static void write(ChannelHandlerContext ctx, FullHttpRequest request, HttpResponseStatus httpResponseStatus) {

        DefaultFullHttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), httpResponseStatus);
        HttpHeaders headers = response.headers();
        headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
        headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "*");
        if (ctx.channel().isWritable()) {
            ChannelFuture channelFuture = ctx.writeAndFlush(response);
            channelFuture.addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    future.channel().close();
                }
            });
        }

    }

    public static void write(ChannelHandlerContext ctx, FullHttpRequest request, ByteBuf content, HttpHeaders headers) {
        DefaultFullHttpResponse response = null;
        if (content != null) {
            response = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.OK, content);
        } else {
            response = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.OK);
        }
        response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
        response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "*");
        if (headers != null) {
            response.headers().setAll(headers);
        }

        if (ctx.channel().isWritable()) {
            ChannelFuture channelFuture = ctx.writeAndFlush(response);
            channelFuture.addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    future.channel().close();
                }
            });

        } else {
            ctx.channel().close();
        }
    }

    public static List<String> getCookies(Cookie... cookies) {
        try {
            ServerCookieEncoder encoder = ServerCookieEncoder.STRICT;
            return encoder.encode(cookies);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getCookie(Cookie cookies) {
        try {
            ServerCookieEncoder encoder = ServerCookieEncoder.STRICT;
            return encoder.encode(cookies);
        } catch (Exception e) {
            return null;
        }
    }

    public static List<Cookie> getCookie(String cookies) {
        try {

            ServerCookieDecoder decoder = ServerCookieDecoder.STRICT;
            return decoder.decodeAll(cookies);
        } catch (Exception e) {
            return null;
        }
    }



    public static void main(String[] args){

        DefaultCookie defaultCookie = new DefaultCookie("name", "value");
        defaultCookie.setMaxAge(10);
        List<String> cookies = getCookies(defaultCookie);
        System.out.println(HttpHeaderNames.SET_COOKIE);
        System.out.println(cookies);

        System.out.println(getCookie("name=1234;nnn=333"));


    }
}
